package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Log;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.persistence.AccountDAO;
import org.csu.mypetstore.persistence.Impl.AccountDAOImpl;
import org.csu.mypetstore.persistence.Impl.LogDAOImpl;
import org.csu.mypetstore.persistence.LogDAO;

import java.util.List;

//账号相关
public class AccountService {
    private AccountDAO accountDAO;
    private LogDAO logDAO;

    public AccountService() {
        accountDAO = new AccountDAOImpl();
        logDAO = new LogDAOImpl();
    }

    //根据username获得account
    public Account getAccount(String username) {
        return accountDAO.getAccountByUsername(username);
    }

    //根据username、password获得account
    public Account getAccount(String username, String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return accountDAO.getAccountByUsernameAndPassword(account);
    }

    //插入新account
    public boolean insertAccount(Account account) {
        //判断数据库中是否已经包含这个username
        Account account1 = accountDAO.getAccountByUsername(account.getUsername());
        System.out.println("准备插入");
        if(account1 == null){
            accountDAO.insertAccount(account);
            System.out.println("插入到Account");
            accountDAO.insertProfile(account);
            System.out.println("插入到profile");
            accountDAO.insertSignon(account);
            System.out.println("插入到signon");
            System.out.println("插入成功啦");
            return true;
        }
        else {
            //插入失败
            System.out.println("已存在该用户，插入失败");
            return false;
        }

    }

    //更新account
    public void updateAccount(Account account) {
        accountDAO.updateAccount(account);
        accountDAO.updateProfile(account);

        if (account.getPassword() != null && account.getPassword().length() > 0) {
            accountDAO.updateSignon(account);
        }
    }

    //添加log
    public void addLog(Log log){
        logDAO.addLog(log);
    }

    //根据username获得log
    public List<Log> getLOGsByUsername(String username) {
        return logDAO.getLogsByUsername(username);
    }


}
